package oldcode;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.thrift.protocol.TMessage;
import org.apache.thrift.protocol.TMessageType;

import thrift.hello.HelloService;
import thrift.hello.HelloService.hello_args;

import com.to8to.kitt.ThriftBizServerChannelInitializer;
import com.to8to.kitt.ThriftClassFactory;
import com.to8to.kitt.ThriftMessage;
import com.to8to.kitt.utils.LogUtils;

public class HelloNettyClient {
    
	ClientChannelHandler clientChannelHandler = new ClientChannelHandler();
    
	ChannelPool channelPool; 
	
    ThriftBizServerChannelInitializer channelInitilizer = new ThriftBizServerChannelInitializer(  clientChannelHandler);
	
	Map<Integer, Integer> msgs = new ConcurrentHashMap<Integer, Integer>();
	
	int maxchannel = 8;
	
	public HelloNettyClient(String host, int port)
	{
        channelInitilizer.setLog(false);
        
        Bootstrap b = new Bootstrap();
        b.group(new NioEventLoopGroup())
         .channel(NioSocketChannel.class)
         .option(ChannelOption.TCP_NODELAY, true)
         .option(ChannelOption.SO_KEEPALIVE, true)
         .handler(channelInitilizer);
        
        channelPool = new ChannelPool(b, host, port,maxchannel);
        
        clientChannelHandler.channelPool = channelPool;
	}
	
	
	public static class MyListener implements ChannelFutureListener{
		
		public int seqid;
		public Map<Integer, Integer> msgs;
		
		@Override
		public void operationComplete(ChannelFuture future)
				throws Exception {
			if(future.isSuccess())
				msgs.put(seqid, 1);
		}
    }
	
	
	public void send(int sendNum)
	{
    	clientChannelHandler.reset();
    	msgs.clear();
    	clientChannelHandler.msgs = msgs;
    	
		long t1 = System.currentTimeMillis();
    	
        int seqid = 0;
        
        for(int i = 0 ; i < sendNum; ++ i)
        {
            ThriftMessage msg = new ThriftMessage();
            
            msg.setHead(new TMessage(HelloService.hello_name, TMessageType.CALL, ++ seqid));
            
            msgs.put(seqid, 1);
            
            msg.setBody(new hello_args("jay.zhang"));
            
            MyListener l = new MyListener();
            
            l.seqid = seqid;
            
            l.msgs = msgs;
            
            channelPool.send(msg, seqid, l);
        }
        

        while(true)
        {
        	try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        	
        	if(clientChannelHandler.isdone)
        		break;
        }
        
        long t2 = System.currentTimeMillis();
        
        System.out.println("total time: " + (t2-t1) + " ms for num " + sendNum);
	}
	
	
	public static void main(String[] args) throws InterruptedException {
		
		ThriftClassFactory.init(HelloService.class);
		
		LogUtils.resetConfigByPath("d:\\logback.xml");
		
		HelloNettyClient c1 = new HelloNettyClient("192.168.3.147", 11111);
//		HelloNettyClient c1 = new HelloNettyClient("192.168.3.19", 30000);
		
//		HelloNettyClient c1 = new HelloNettyClient("192.168.3.147", 2222);
		
		c1.send(500000);
		
//		c2.send(2);
		System.exit(0);
	}

}
